﻿using System;
using System.Data;
using BO.Infrastructure.StaticData;
using BO.Infrastructure.Utils;

namespace GUI.Controls.News
{
    public partial class HighlightNewsAtHomePage : System.Web.UI.UserControl
    {
        const int Top = 2;
        const string Format =
            "<li>{3}" +
                "<h2><a href=\"{0}\" title=\"{2}\">{1}{4}</a></h2>" +
            "</li>";

        public bool DataBind(DataTable dt)
        {
            if (dt != null && dt.Rows.Count > 0)
            {
                var imageWidth = default(int);
                var imageHeight = default(int);
                for (var i = 0; i < Math.Min(dt.Rows.Count, Top); i++)
                {
                    switch (i)
                    {
                        case 0:
                            imageWidth = 410;
                            imageHeight = 230;
                            break;
                        case 1:
                            imageWidth = 235;
                            imageHeight = 230;
                            break;
                        case 2:
                        case 3:
                        case 4:
                            imageWidth = 210;
                            imageHeight = 158;
                            break;
                    }

                    var r = dt.Rows[i];

                    var avatar = PresentationUtils.Photo.BuildNewsAvatar(r, imageWidth, imageHeight);

                    var text = string.Format(
                               Format,
                               r[SqlConstants.NewsPublish.Url],
                               r[SqlConstants.NewsPublish.Title],
                               r[SqlConstants.NewsPublish.EncodedTitle],
                               avatar,
                               r[SqlConstants.NewsPublish.NewsTypeIcon]
                            );
                    if (i <= 1)//2 tin trên
                    {
                        ltrRow1.Text += text;
                    }
                    else//3 tin dưới
                    {
                        ltrRow2.Text += text;
                    }
                }
                return true;
            }
            return false;
        }

    }
}